﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.StockCountServiceRef;
    using WMS6.Web.UserControls;

    public class PCMaintenanceList : BasePage
    {
        protected Button btnAction;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnReSearch;
        protected Button btnSave;
        protected Button btnSearch;
        protected HtmlForm F5552;
        protected GridView gvPCMaintenanceList;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataphysicalTagList(PhysicalTagQueryEntity physicalTagQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(physicalTagQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = StockCountServiceClientProxy.GetInstance().GetPhysicalTagDAByQueryEntity(physicalTagQueryEntity);
            base.BindData2GridView(this.gvPCMaintenanceList, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPCMaintenanceList, "PHYSICAL_REQUEST_ID");
            List<string> selectedTagNoID = base.GetSelectedIDsInGridView(this.gvPCMaintenanceList, "TAG_NO");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("~/Inventory/PCMaintenance.aspx?from=list&ID={0}&TagNo={1}", base.UrlEncode(selectedID[0]), base.UrlEncode(selectedTagNoID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity TagQueryEntity = this.GetphysicalTagQueryEntity();
            TagQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = TagQueryEntity.CurrentPage;
            TagQueryEntity.CurrentPage = "0";
            this.gvPCMaintenanceList.Columns[1].Visible = false;
            this.gvPCMaintenanceList.AllowSorting = false;
            this.BindDataphysicalTagList(TagQueryEntity);
            this.hdnActionID.Value = "";
            this.gvPCMaintenanceList.ShowFooter = false;
            TagQueryEntity.IsGetAll = false;
            TagQueryEntity.CurrentPage = currentPageIndex;
            this.SetphysicalTagQueryEntity(TagQueryEntity);
            base.Export2Excel("PCMaintenance.xls", this.gvPCMaintenanceList);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
            physicalTag.CurrentPage = "0";
            this.BindDataphysicalTagList(physicalTag);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
            physicalTag.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataphysicalTagList(physicalTag);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity TagQueryEntity = this.GetphysicalTagQueryEntity();
            TagQueryEntity.PhysicalRequestID = "";
            TagQueryEntity.TagNo = "";
            TagQueryEntity.GroupNo = "";
            TagQueryEntity.OwnerID = "";
            TagQueryEntity.SkuID = "";
            TagQueryEntity.Loc = "";
            TagQueryEntity.Lot = "";
            TagQueryEntity.LpnID = "";
            TagQueryEntity.CountQty = "";
            TagQueryEntity.TaskDetailID = "";
            this.BindDataphysicalTagList(TagQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
            if (int.Parse(physicalTag.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                physicalTag.CurrentPage = (int.Parse(physicalTag.CurrentPage) + 1).ToString();
                this.BindDataphysicalTagList(physicalTag);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
            if (int.Parse(physicalTag.CurrentPage) > 0)
            {
                physicalTag.CurrentPage = (int.Parse(physicalTag.CurrentPage) - 1).ToString();
                this.BindDataphysicalTagList(physicalTag);
            }
        }

        protected void btnSave_Click(object sener, EventArgs e)
        {
            List<PhysicalTagInfo> physicalTagList = new List<PhysicalTagInfo>();
            PhysicalTagInfo physicalTagInfo = null;
            try
            {
                for (int i = 0; i < this.gvPCMaintenanceList.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvPCMaintenanceList.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        if (!(string.IsNullOrEmpty(this.gvPCMaintenanceList.DataKeys[i]["STATUS"].ToString()) || ((this.gvPCMaintenanceList.DataKeys[i]["STATUS"].ToString().ToUpper() != "PRINTED") && !(this.gvPCMaintenanceList.DataKeys[i]["STATUS"].ToString().ToUpper() == "CRELEASED"))))
                        {
                            physicalTagInfo = new PhysicalTagInfo {
                                PhysicalRequestID = this.gvPCMaintenanceList.DataKeys[i]["PHYSICAL_REQUEST_ID"].ToString(),
                                TagNo = Convert.ToInt32(this.gvPCMaintenanceList.DataKeys[i]["TAG_NO"]),
                                CountDate = DateTime.Now,
                                CountBy = base.UserInfo.UserID,
                                Status = "Counted"
                            };
                            if (!string.IsNullOrEmpty(((TextBox) this.gvPCMaintenanceList.Rows[i].FindControl("txtCountQyt")).Text))
                            {
                                physicalTagInfo.CountQty = decimal.Parse(((TextBox) this.gvPCMaintenanceList.Rows[i].FindControl("txtCountQyt")).Text);
                            }
                            physicalTagList.Add(physicalTagInfo);
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5770_003");
                            return;
                        }
                    }
                }
                if (physicalTagList.Count > 0)
                {
                    string strValue = StockCountServiceClientProxy.GetInstance().UpdatePhysicalTagList(physicalTagList, "COUNT");
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5770_004") + strValue + base.GetGlobalMsgResource("F5770_005");
                    PhysicalTagQueryEntity tagQueryEntity = this.GetphysicalTagQueryEntity();
                    this.BindDataphysicalTagList(tagQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private PhysicalTagQueryEntity GetphysicalTagQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PhysicalTagQueryEntity);
            }
            return (base.PageQueryEntitySession as PhysicalTagQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
                physicalTag.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataphysicalTagList(physicalTag);
            }
        }

        protected void gvPCMaintenanceList_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5552", this.gvPCMaintenanceList);
        }

        protected void gvPCMaintenanceList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                ((TextBox) e.Row.FindControl("txtCountQyt")).Attributes.Add("onblur", "CheckTrue('" + e.Row.FindControl("chbSelect").ClientID + "',this,'" + ((TextBox) e.Row.FindControl("txtCountQyt")).Text + "');");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvPCMaintenanceList_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PhysicalTagQueryEntity physicalTag = this.GetphysicalTagQueryEntity();
                physicalTag.SortField = e.SortExpression;
                if (physicalTag.SortDirection == "ASC")
                {
                    physicalTag.SortDirection = "DESC";
                }
                else if (physicalTag.SortDirection == "DESC")
                {
                    physicalTag.SortDirection = "ASC";
                }
                this.BindDataphysicalTagList(physicalTag);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PhysicalTagQueryEntity physicalTag = new PhysicalTagQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    physicalTag.PhysicalRequestID = this.PreviousPage.PhysicalRequestID;
                    physicalTag.TagNo = this.PreviousPage.TagNo;
                    physicalTag.TeamNo = this.PreviousPage.TeamNo;
                    physicalTag.OwnerID = this.PreviousPage.OwnerID;
                    physicalTag.SkuID = this.PreviousPage.SkuID;
                    physicalTag.Loc = this.PreviousPage.Loc;
                    physicalTag.Lot = this.PreviousPage.Lot;
                    physicalTag.LpnID = this.PreviousPage.LpnID;
                    physicalTag.CountQty = this.PreviousPage.CountQTY;
                    physicalTag.TaskDetailID = this.PreviousPage.TaskDetailID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            physicalTag.IsGetAll = false;
            physicalTag.CurrentPage = "0";
            physicalTag.PageSize = this.gvPCMaintenanceList.PageSize.ToString();
            physicalTag.SortField = " PT.PHYSICAL_REQUEST_ID DESC ,PT.TAG_NO";
            physicalTag.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvPCMaintenanceList.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetphysicalTagQueryEntity(physicalTag);
            this.BindDataphysicalTagList(physicalTag);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetphysicalTagQueryEntity(PhysicalTagQueryEntity physicalTagQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = physicalTagQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = physicalTagQueryEntity;
            }
        }

        public PCMaintenanceQuery PreviousPage
        {
            get
            {
                return (PCMaintenanceQuery) base.PreviousPage;
            }
        }
    }
}

